<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ajax extends Controller {
	public function before() {
        if (!Cookie::get('lang', NULL)) {
            Cookie::set('lang', 'ru');
            View::set_global('lang', '');
            Controller_Common::$postfix = '';
        } else {
            View::set_global('lang', Cookie::get('lang', NULL));
            Cookie::get('lang', NULL) == 'ru' ? Controller_Common::$postfix = '' : Controller_Common::$postfix = '_'.Cookie::get('lang', NULL);
        }
	}

	public function action_submenu() {
		if (!$this->request->is_ajax()) {
			die('No direct script access.');
		}

		$submenu = Model::factory('Menu')->get_submenu($this->request->post('id'));
		$data = '<div class="submenu_container" pid="'.$this->request->post('id').'">';

		foreach ($submenu as $key => $item) {
			if ($item->link == 'javascript:void(0)') {
				$data .= '<div style="margin-top: 1em"><a href="'.$item->link.'" class="underline with_childrens" onclick="show_submenu(this, '.$item->id.')">'.$item->title.'</a><span>&nbsp;&raquo;</span></div>';
			} else {
				$data .= '<div style="margin-top: 1em"><a href="'.$item->link.'" class="underline">'.$item->title.'</a></div>';
			}
		}

		$data .= '</div>';
		echo json_encode(array('status' => 'ok', 'data' => $data));
	}
}